package com.weixingzh.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.weixingzh.entity.GetWxTokenBean;
import com.weixingzh.entity.WxTokenBean;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

/**
 * @FileName GetWxToken
 * @Description
 * @Author Me
 * @date 2024-07-19
 **/

@Component
public class GetWxToken {
    public RestTemplate request;   //使用RestTemplate发送请求
    @Autowired
    private GetWxTokenBean gtn;
//    public static String access_token;

    public GetWxToken() {
//        access_token = getToken().getAccess_token();
    }

    //获取access_token
    public JSONObject getToken(){
        request = new RestTemplate();
        StringBuilder url = new StringBuilder("https://api.weixin.qq.com/cgi-bin/token?");
        url.append("grant_type=").append(gtn.grant_type).append("&");
        url.append("appid=").append(gtn.appid).append("&");
        url.append("secret=").append(gtn.secret);
        String result = request.getForObject(new String(url), String.class);

        return JSON.parseObject(result);
    }

}
